<?php
/**
 *
 * Auth: feikor
 * Date: 2023/06/12
 */

namespace app\admin\controller;

use app\admin\service\VideoService;
use app\model\VideoCustom;
use app\model\VideoDetails;
use think\facade\Request;
use think\facade\Session;

class My extends Base
{
	private $service;

	public function __construct(VideoService $service)
	{
		parent::__construct();
		$this->service = $service;
	}

	/**
	 * 渲染列表
	 */
	public function index()
	{
		$params = Request::param();
		$where = [];
		if (! empty($params['drama_id'])) {
			$where[] = [
				'drama_id',
				'=',
				$params['drama_id']
			];
		}
		if (! empty($params['type'])) {
			$where[] = [
				'type_id',
				'=',
				$params['type']
			];
		}
		$where[] = [
			'admin_id',
			'=',
			Session::get('user_id')
		];
		if (isset($params['drama_id']) && isset($params['type'])) {
			$custom = (new VideoDetails())->where($where)->distinct('true')->field('custom_id')->select()->toArray();
			if (empty($custom)) {
				$data = [];
			} else {
				$custom_ids = '';
				foreach ($custom as $item) {
					$custom_ids .= $item['custom_id'] . ',';
				}
				$custom_ids = explode(',', $custom_ids);
				$custom_ids = array_filter($custom_ids);
				$data = (new VideoCustom())->where(['id' => $custom_ids])->select()->toArray();
			}
		} else {
			$data = [];
		}
		$dramalist = $this->service->getAllDrama();
		$typelist = $this->service->getAllType();
		return view('index', [
			'rule_id'   => Session::get('rule_id'),
			'data'      => $data,
			'dramalist' => $dramalist,
			'typelist'  => $typelist,
			'drama_id'  => isset($params['drama_id']) ? $params['drama_id'] : 0,
			'type_id'   => isset($params['type']) ? $params['type'] : 0,
		]);
	}
}